package oas.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import oas.domain.User;
import org.apache.log4j.Logger;

/**
 * 页面主题内容的simpletag
 *
 * @author wjy
 * @version v1.1.0
 */
public class ContentSimpleTag extends SimpleTagSupport {

    private String contextUrl = null;

    private void navForAdmin(StringBuilder outMsg) {
        // 左侧导航栏
        outMsg.append("<div style=\"position: -webkit-sticky; position: sticky; top: 60px;\"> \n");
        outMsg.append("<div class=\"nav navbar navbar-header\"> \n");
        outMsg.append("OAS \n");
        outMsg.append("</div> \n");
        outMsg.append("<ul class=\"nav nav-pills flex-column\"> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("\">主页</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/employee\">管理员工</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/manager/leave\">审批请假</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/leaveTypes\">维护请假类型</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/workcells\">管理部门</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/employee/attendances\">查看考勤记录</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("</ul> \n");
        outMsg.append("</div> \n");
    }

    private void navForEmployee(StringBuilder outMsg) {
        // 左侧导航栏
        outMsg.append("<div style=\"position: -webkit-sticky; position: sticky; top: 60px;\"> \n");
        outMsg.append("<div class=\"nav navbar navbar-header\"> \n");
        outMsg.append("OAS \n");
        outMsg.append("</div> \n");
        outMsg.append("<ul class=\"nav nav-pills flex-column\"> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/\">主页</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/attendances\">考勤</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/leave\">请假</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/attendances/history\">我的考勤</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("<li class=\"nav-item\"> \n");
        outMsg.append("<a class=\"nav-link\" href=\"").append(contextUrl).append("/leave/history\">我的请假</a> \n");
        outMsg.append("</li> \n");
        outMsg.append("</ul> \n");
        outMsg.append("</div> \n");
    }

    @Override
    public void doTag() throws JspException, IOException {

        JspFragment jspBody = getJspBody();
        HttpSession session = ((PageContext) this.getJspContext()).getSession();
        HttpServletRequest req = (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest();
        User user = (User) session.getAttribute("user");
        JspWriter out = getJspContext().getOut();
        contextUrl = req.getContextPath();

        StringBuilder outMsg = new StringBuilder();

        outMsg.append(PART_0);

        if (user != null && "Manager".equals(user.getRole())) {
            navForAdmin(outMsg);
        } else {
            navForEmployee(outMsg);
        }

        outMsg.append(PART_1);
        out.write(outMsg.toString());
        out.flush();
        // 内容主体
        jspBody.invoke(null);

        // 结尾
        outMsg = new StringBuilder();
        outMsg.append(PART_2);

        out.write(outMsg.toString());
        out.flush();

    }

    private static final String PART_0 = "<div class=\"container-fluid\"> \n" +
            "<div class=\"row\" style=\"min-height:400px; margin-top: 60px\"> \n" +
            "<div class=\"col-sm-2\" style=\"overflow: visible; max-width: 240px\"> \n";

    private static final String PART_1 = "</div> \n" +
            "<div class=\"col-sm-10\"  style=\"background-color:#ffffff\"> \n";

    private static final String PART_2 = "</div> \n" + "</div> \n" + "</div> \n";
}
